package com.kongzue.baseokhttpv3.newslice;


import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.listener.ParameterInterceptListener;
import com.kongzue.baseokhttp.listener.ResponseInterceptListener;
import com.kongzue.baseokhttp.util.*;
import com.kongzue.baseokhttpv3.ResourceTable;
import com.kongzue.baseokhttpv3.util.JsonUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import okhttp3.MediaType;
import org.apache.http.util.TextUtils;

import java.util.HashMap;
import java.util.Map;


public class DeleteAbilitySlice extends AbilitySlice {
    private Button deletebiaodan, deletejson;
    private TextField biaodanurl, biaodaninput, jsonurl, jsoninput, cookie;
    private Text content;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_dpp_new);
        header();
        initView();
        initClick();
    }

    private void initClick() {

    }

    private void header() {

        BaseOkHttpConfig.getInstance().setParameterInterceptListener(new ParameterInterceptListener<JsonMap>() {
            @Override
            public JsonMap onIntercept(Context context, String url, JsonMap o) {
                return null;
            }
        });
        BaseOkHttpConfig.getInstance().setResponseInterceptListener(new ResponseInterceptListener() {
            @Override
            public boolean onResponse(Context context, String url, String response,
                                      Exception error) {
                if (error != null) {
                    return true;
                } else {
                    return true;
                }
            }
        });

        JsonMapConfig.getInstance().setPreParsing(true);
    }

    private void initView() {
        deletebiaodan = (Button) findComponentById(ResourceTable.Id_delete_biaodan_button);
        biaodanurl = (TextField) findComponentById(ResourceTable.Id_delete_biaodan_url);
        biaodaninput = (TextField) findComponentById(ResourceTable.Id_delete_biaodan_input);
        deletejson = (Button) findComponentById(ResourceTable.Id_delete_json_button);
        jsonurl = (TextField) findComponentById(ResourceTable.Id_delete_json_url);
        jsoninput = (TextField) findComponentById(ResourceTable.Id_delete_json_input);
        cookie = (TextField) findComponentById(ResourceTable.Id_cookie_input);
        cookie.setText("header_value");
        content = (Text) findComponentById(ResourceTable.Id_back_content);
        biaodaninput.setText("{\n" +
                "    \"key\": \"177c070d1922f938ccee95b6bd04df0b\",\n" +
                "    \"city\": \"上海市\"\n" +
                "}");
        biaodanurl.setText("/mock/d2ac8d0e4404412de7b2b4e23f026c63/_ceshi-01/delete/test");
        jsoninput.setText("{\n" +
                "    \"key\": \"177c070d1922f938ccee95b6bd04df0b\",\n" +
                "    \"city\": \"上海市\"\n" +
                "}");
        jsonurl.setText("/mock/d2ac8d0e4404412de7b2b4e23f026c63/_ceshi-01/delete/test");
        deletebiaodan.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                try {
                    if (!TextUtils.isEmpty(biaodanurl.getText().toString())) {
                        Map<String, String> params = new HashMap<>();
                        if (!TextUtils.isEmpty(biaodaninput.getText().toString())) {
                            String json = biaodaninput.getText().toString();

                            Map<String, Object> jsonObject = null;
                            try {
                                jsonObject = JsonUtil.jsonToMap(json);
                                for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                                    params.put(entry.getKey(), entry.getValue().toString());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        getbiaodan(biaodanurl.getText().toString(), params);

                    } else {
                        ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                        toastDialog.setText("url is null").setDuration(1000).show();
                    }
                } catch (Exception e) {
                    ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                    toastDialog.setText("Invalid forma").setDuration(1000).show();
                }
            }
        });


        deletejson.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                try {
                    MediaType JSON = MediaType.parse("application/json;charset=utf-8");

                    if (!TextUtils.isEmpty(jsonurl.getText().toString())) {
                        if (TextUtils.isEmpty(jsoninput.getText().toString())) {
                            ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                            toastDialog.setText("no is json").setDuration(1000).show();
                            return;
                        }
                        deleteJson(jsonurl.getText().toString(), jsoninput.getText());
                    } else {
                        ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                        toastDialog.setText("url is null").setDuration(1000).show();
                    }
                } catch (Exception e) {
                    ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                    toastDialog.setText("Invalid forma").setDuration(1000).show();
                }
            }
        });
    }

    private void getbiaodan(final String url, Map<String, String> params) {
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);
        Parameter parameter = new Parameter();
        if (null != params) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                parameter.add(entry.getKey(), entry.getValue());
            }
        }

        Parameter header = new Parameter();
        header.add("test1", cookie.getText());

        HttpRequest.DELETE(DeleteAbilitySlice.this, url,
                header, parameter, new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {
                        if (error == null) {
                            DeleteAbilitySlice.this.getContext().getMainTaskDispatcher().asyncDispatch(new Runnable() {
                                @Override
                                public void run() {
                                    ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                                    toastDialog.setText("ok").setDuration(1000).show();
                                    content.setText(main.toString());


                                }
                            });
                            //
                        } else {
                            ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                            toastDialog.setText("time out").setDuration(1000).show();
                        }
                    }
                });

    }

    private void deleteJson(final String url, String json) {
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);

        Parameter header = new Parameter();
        header.add("headerkey", cookie.getText());
        HttpRequest.build(DeleteAbilitySlice.this, url).setJsonParameter(json).setHeaders(header).setJsonResponseListener(new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (error == null) {
                    DeleteAbilitySlice.this.getContext().getMainTaskDispatcher().asyncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                            toastDialog.setText("OK").setDuration(1000).show();
                            content.setText(main.toString());


                        }
                    });
                } else {
                    ToastDialog toastDialog = new ToastDialog(DeleteAbilitySlice.this);
                    toastDialog.setText("time out").setDuration(1000).show();
                }
            }
        }).doDelete();
    }

}
